/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.enumeration;

import com.huize.ladon.apm.common.enums.EnumInterface;
import org.apache.commons.lang3.StringUtils;

import java.util.stream.Stream;

/**
 * 描述: 统计类型枚举
 *
 * @author tianyuliang
 * @version $Id: StatTypeEnum.java, v0.1 2021/4/9
 */
public enum StatTypeEnum implements EnumInterface {

    /**
     * 未知
     */
    UNKNOWN("unknown", "未知"),

    /**
     * 实时统计(10分钟类型)
     */
    REAL_TIME("realtime", "实时统计"),

    /**
     * 延迟统计(T+1天)
     */
    DELAY("delay", "延迟统计"),

    ;

    private String type;

    private String desc;

    StatTypeEnum(String type, String desc) {
        this.type = type;
        this.desc = desc;
    }

    public static StatTypeEnum of(String code) {
        if (StringUtils.isEmpty(code)) {
            return UNKNOWN;
        }

        return Stream.of(values()).filter(b -> b.getCode().equalsIgnoreCase(code.trim())).findFirst().orElseGet(() -> UNKNOWN);
    }

    /**
     * 校验是否有效 true:有效, false:无效
     */
    public static boolean checkValidType(String type) {
        StatTypeEnum typeEnum = of(type);
        return typeEnum != UNKNOWN;
    }

    /**
     * 校验是否有效 true:有效, false:无效
     */
    public static boolean checkValidType(StatTypeEnum typeEnum) {
        return typeEnum != UNKNOWN;
    }

    /**
     * 校验是否为“实时统计”
     */
    public static boolean isRealTime(StatTypeEnum typeEnum) {
        return typeEnum != null && typeEnum == REAL_TIME;
    }

    /**
     * 校验是否为“延迟统计”
     */
    public static boolean isDelay(StatTypeEnum typeEnum) {
        return typeEnum != null && typeEnum == DELAY;
    }

    /**
     * 校验是否为“实时统计”
     */
    public static boolean isRealTime(String type) {
        return of(type) == REAL_TIME;
    }

    /**
     * 校验是否为“延迟统计”
     */
    public static boolean isDelay(String type) {
        return of(type) == DELAY;
    }

    public static String resolveDesc(StatTypeEnum typeEnum) {
        return typeEnum == null ? "未知" : (typeEnum == REAL_TIME ? "实时T0" : "离线T1");
    }

    public String getType() {
        return type;
    }

    @Override
    public String getCode() {
        return type;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return String.format("[%s,%s]", getCode(), getDesc());
    }

}